// pages/myinfo/myinfo.js
var app = getApp();
var c = require("../libs/common.js");
var config = require('../libs/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    indexsex: 0,
    head: '/images/my1.png',
    widthinfo: '100rpx',
    date: '2016-09-01',
    sex: ['男孩', '女孩'],
    index: 0,
    jue: ['否', '是'],
    indexjue: 0,
    petpj: ['猫咪', '狗狗'],
    classtype: '选择',
    indexpj: 0,
    maostatus: true,
    goustatus: true,
    fangyi: true,
    typeinfo: true,
    datebirthday: '2018-01-01',
    kqb: [],
    crb: [],
    tnqc: [],
    twqc: []
  },
  bindDateChangekqb: function(e) {
    var date = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var kqb = this.data.kqb;
    var kqbindex = 'kqb[' + index + ']';
    this.setData({
      [kqbindex]: date
    })
  },
  bindDateChangekqbadd: function(e) {
    var date = e.detail.value;
    var len = this.data.kqb.length - 1;
    var kqbindex = 'kqb[' + len + ']';
    this.setData({
      [kqbindex]: date
    })
  },
  bindDateChangecrb: function(e) {
    var date = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var crb = this.data.crb;
    var crbindex = 'crb[' + index + ']';
    this.setData({
      [crbindex]: date
    })
  },
  bindDateChangecrbadd: function(e) {
    var date = e.detail.value;
    var len = this.data.crb.length - 1;
    var crbindex = 'crb[' + len + ']';
    this.setData({
      [crbindex]: date
    })
  },
  bindDateChangetnqc: function(e) {
    var date = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var tnqc = this.data.tnqc;
    var tnqcindex = 'tnqc[' + index + ']';
    this.setData({
      [tnqcindex]: date
    })
  },
  bindDateChangetnqcadd: function(e) {
    var date = e.detail.value;
    var len = this.data.tnqc.length - 1;
    var tnqcindex = 'tnqc[' + len + ']';
    this.setData({
      [tnqcindex]: date
    })
  },
  bindDateChangetwqc: function(e) {
    var date = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var twqc = this.data.twqc;
    var twqcindex = 'twqc[' + index + ']';
    this.setData({
      [twqcindex]: date
    })
  },
  bindDateChangetwqcadd: function(e) {
    var date = e.detail.value;
    var len = this.data.twqc.length - 1;
    var twqcindex = 'twqc[' + len + ']';
    this.setData({
      [twqcindex]: date
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    c.loginVer();
    var id = options.id; //如果 id=undefine 为添加 否则为详情
    c.ajax(
      config.hostapi + 'Pet/GetPetClassInfo', {},
      res => {
        this.setData({
          gou: res.data.data.gou
        });
        this.setData({
          mao: res.data.data.mao
        });
      }
    )

    if (typeof (id) !='undefined'){
      this.setData({
        id: id
      });
       c.ajax(
         config.hostapi +'Pet/ToPidGetPetInfo',
         {id:id},
         res=>{
           var petinfo=res.data.data;
           console.log(res.data.data);
           this.setData({ classtype: petinfo.classtype, head: petinfo.p_head, name: petinfo.p_name, indexsex: petinfo.p_sex, datebirthday: petinfo.p_birthday, indexjue: petinfo.p_jue, kqb: petinfo.p_kqb, crb: petinfo.p_crb, tnqc: petinfo.p_tnqc, twqc: petinfo.p_twqc, typeid: petinfo.p_type })
         }
       )
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //昵称
  blurname: function(e) {
    var value = e.detail.value;
    this.setData({
      name: value
    });

  },
  //性别
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为1', e.detail.value)
    var index = e.detail.value;
    this.setData({
      indexsex: index
    })
  },
  //是否绝育
  bindPickerChangejue: function(e) {
    console.log('picker发送选择改变，携带值为1', e.detail.value)
    var index = e.detail.value;
    this.setData({
      indexjue: index
    })
  },
  bindPickerChangepj: function(e) {
    var index = e.detail.value;
    this.setData({
      indexpj: e.detail.value
    })
    if (index == 0) {
      this.setData({
        maostatus: false,
        goustatus: true,
        typeinfo: false
      });
    } else {
      this.setData({
        maostatus: true,
        goustatus: false,
        typeinfo: false
      });
    }
  },
  //日期
  bindDateChangebirthday: function(e) {
    console.log('picker发送选择改变，携带值为2', e.detail.value)
    var value = e.detail.value;
    this.setData({
      datebirthday: e.detail.value
    })
  },
  clickmao: function(e) {
    var name = e.currentTarget.dataset.name;
    var typeid = e.currentTarget.dataset.id;
    this.setData({
      maostatus: true,
      goustatus: true,
      typeinfo: true,
      classtype: name,
      typeid: typeid
    })
  },
  clickgou: function(e) {
    var name = e.currentTarget.dataset.name;
    var typeid = e.currentTarget.dataset.id;
    this.setData({
      maostatus: true,
      goustatus: true,
      typeinfo: true,
      classtype: name,
      typeid: typeid
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindDateDelete: function(e) {
    var index = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      success(res) {
        if (res.confirm) {
          var kqb = that.data.kqb;
          var kqbnew = kqb.splice(index, 1);
          that.setData({
            kqb: kqb
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  bindDateDeletecrb: function(e) {
    var index = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      success(res) {
        if (res.confirm) {
          var crb = that.data.crb;
          var crbnew = crb.splice(index, 1);
          that.setData({
            crb: crb
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  bindDateDeletetnqc: function(e) {
    var index = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      success(res) {
        if (res.confirm) {
          var tnqc = that.data.tnqc;
          var tnqcnew = tnqc.splice(index, 1);
          that.setData({
            tnqc: tnqc
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  bindDateDeletetwqc: function(e) {
    var index = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      success(res) {
        if (res.confirm) {
          var twqc = that.data.twqc;
          var twqcnew = twqc.splice(index, 1);
          that.setData({
            twqc: twqc
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  bindDateAdd: function() {
    var myDate = new Date(); //获取系统当前时间
    var y = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var m = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var d = myDate.getDate(); //获取当前日(1-31)
    var date = y + '-' + m + '-' + d;
    console.log(date);
    var kqb = this.data.kqb;
    var newData = date;
    kqb.push(newData);
    var index = kqb.length;
    this.setData({
      kqb: kqb
    })

  },
  bindDateAddcrb: function() {
    var myDate = new Date(); //获取系统当前时间
    var y = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var m = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var d = myDate.getDate(); //获取当前日(1-31)
    var date = y + '-' + m + '-' + d;
    var crb = this.data.crb;
    var newData = date;
    crb.push(newData);
    var index = crb.length;
    this.setData({
      crb: crb
    })

  },
  bindDateAddtnqc: function() {
    var myDate = new Date(); //获取系统当前时间
    var y = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var m = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var d = myDate.getDate(); //获取当前日(1-31)
    var date = y + '-' + m + '-' + d;
    var tnqc = this.data.tnqc;
    var newData = date;
    tnqc.push(newData);
    var index = tnqc.length;
    this.setData({
      tnqc: tnqc
    })

  },
  bindDateAddtwqc: function() {
    var myDate = new Date(); //获取系统当前时间
    var y = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var m = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var d = myDate.getDate(); //获取当前日(1-31)
    var date = y + '-' + m + '-' + d;
    var twqc = this.data.twqc;
    var newData = date;
    twqc.push(newData);
    var index = twqc.length;
    this.setData({
      twqc: twqc
    })
  },
  fangyiclick: function() {
    wx.navigateTo({
      url: '/pages/fangyi/fangyi',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    // this.setData({
    //   typeinfo: false,
    //   fangyi: false
    // })
  },
  fanhuidelete: function() {
    this.setData({
      fangyi: true,
      typeinfo: true
    })
  },
  imgtap: function() {
    c.getTemImg(this, 'head');
  },
  submit: function() {
    var id= this.data.id;
    var uid = wx.getStorageSync('userinfo').id;
    var head = this.data.head;
    var name = this.data.name;
    var indexsex = this.data.indexsex;
    var datebirthday = this.data.datebirthday;
    var indexjue = this.data.indexjue;
    var indexpj = this.data.indexpj;
    var typeid = this.data.typeid;
    var kqb = this.data.kqb;
    var crb = this.data.crb;
    var tnqc = this.data.tnqc;
    var twqc = this.data.twqc;
    c.ajax(
      config.hostapi + 'Pet/AddOrEditPet', {
        id:id,
        uid: uid,
        head: head,
        name: name,
        indexsex: indexsex,
        datebirthday: datebirthday,
        indexjue: indexjue,
        indexpj: indexpj,
        typeid: typeid,
        kqb: kqb,
        crb: crb,
        tnqc: tnqc,
        twqc: twqc
      },
      res => {
        if (res.data.status == 'success') {
          wx.showToast({
            title: '操作成功',
            image: '/images/dui.png'
          })
          
          setTimeout(function() {
            wx.navigateTo({
              url: '/pages/mypetlist/mypetlist',
            })
          }, 1000)
        } else {
          wx.showToast({
            title: '操作失败',
            image: '/images/closered.png'
          })
       
        };
      }
    )
  }
})